package com.ruoyi.message.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.message.domain.Message;
import com.ruoyi.message.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message/message")
@Anonymous
public class MessageController extends BaseController {
    @Autowired
    private IMessageService messageService;

    @GetMapping("/list")
    public TableDataInfo list(Message message) {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    @PostMapping("/addMessage")
    public AjaxResult addMessage(@RequestBody Message message) {
        // 获取当前登录用户的ID
        Long userId = SecurityUtils.getLoginUser().getUserId();
        message.setUserId(userId);
        // 存储留言
        if (messageService.insertMessage(message) < 1) {
            return AjaxResult.error("留言失败");
        }
        return AjaxResult.success("留言成功");
    }

}

